import request from "../utils/request";
import type { ApiResponse } from "../utils/utils";
import type {
  GetReviewListParams,
  GetOSSSignatureParams,
  OSSSignatureResult,
  SaveAttachmentParams,
  AttachmentResult,
  AuditCaseParams,
  FinalAuditCaseParams,
  GetCaseListParams,
  GetCaseListResult,
  ResponseStatus,
  DistributeStatus as DistributeStatusType,
  GetReviewListResult,
  GetEntrustRetractListParams,
  GetEntrustRetractListResult,
  GetReviewDetailResult,
  LoginParams,
  LoginResult,
  LoginSmsParams,
  LoginSmsResult,
  UserInfo,
  EnumInfo,
  GetEnumListParams,
  ReviewUserResult,
  GetReviewUserParams,
  // 分案相关类型
  CountDistributeOneParams,
  CountDistributeOneResult,
  GetOrgListResult,
  QueryUserByOrgParams,
  QueryUserByOrgResult,
  DistributeOneParams,
  DistributeOneResult,
  // 录案相关类型
  GetRecordCaseListParams,
  GetRecordCaseListResult,
  // 渠道和产品相关类型
  GetChannelListParams,
  GetChannelListResult,
  GetProductListParams,
  GetProductListResult,
} from "./type";

// 接口基础域名
import { CONFIG } from "../constant/config";

// 审核状态

export const CASE_STATUS = {
  TO_AUDIT: {
    code: "TO_AUDIT",
    message: "待复审",
  },
  AUDITING: {
    code: "AUDITING",
    message: "审核中",
  },
  PRE_AUDIT_SUCCESS: {
    code: "PRE_AUDIT_SUCCESS",
    message: "预审通过",
  },
  PRE_AUDIT_FAILED: {
    code: "PRE_AUDIT_FAILED",
    message: "预审不通过",
  },
  AUDIT_SUCCESS: {
    code: "AUDIT_SUCCESS",
    message: "终审通过",
  },
  AUDIT_FAILED: {
    code: "AUDIT_FAILED",
    message: "终审不通过",
  },
  IMPORT_FAILED: {
    code: "IMPORT_FAILED",
    message: "导入失败",
  },
  INVALID: {
    code: "INVALID",
    message: "批次作废",
  },
};

// 获取当前用户的信息
export const getCurrentUserInfo = (): Promise<ApiResponse<UserInfo>> => {
  return request.post(
    `${CONFIG.baseURL}/authority/anon/account/getCurrentUser`
  );
};

// 枚举列表
export const getEnumList = (
  params: GetEnumListParams
): Promise<ApiResponse<EnumInfo>> => {
  return request.post(`${CONFIG.baseURL}/qtong/anon/enum/get`, params);
};

// 获取待审列表
export const getReviewList = (
  params: GetReviewListParams
): Promise<ApiResponse<GetReviewListResult>> => {
  return request.post(
    `${CONFIG.baseURL}/qtong/mng/entrustA/uploadRecord/list`,
    params
  );
};

// 获取待审详情
export const getReviewDetail = (
  id: String
): Promise<ApiResponse<GetReviewDetailResult>> => {
  return request.post(`${CONFIG.baseURL}/qtong/mng/uploadRecordEntrust/info`, {
    id,
  });
};

// 获取待审详情
export const getReviewCaseDetail = (
  params: GetReviewUserParams
): Promise<ApiResponse<ReviewUserResult>> => {
  return request.post(
    `${CONFIG.baseURL}/qtong/mng/entrust/uploadCase/get`,
    params
  );
};

// 1. 文件上传相关接口

// 获取OSS签名
export const getOSSSignature = (
  params: GetOSSSignatureParams
): Promise<ApiResponse<OSSSignatureResult>> => {
  console.log("获取OSS签名:", params);
  return request.post(`${CONFIG.baseURL}/authority/mng/oss/signature`, params);
};

// 保存文件附件
export const saveAttachment = (
  params: SaveAttachmentParams
): Promise<ApiResponse<AttachmentResult>> => {
  return request.post(
    `${CONFIG.baseURL}/blackhole/mng/lawsuit/attachment/save`,
    params
  );
};

// 2. 审核流程相关接口

// 复审
export const auditCase = (
  params: AuditCaseParams
): Promise<ApiResponse<void>> => {
  return request.post(
    `${CONFIG.baseURL}/qtong/mng/uploadRecordEntrust/audit`,
    params
  );
};

// 终审
export const finalAuditCase = (
  params: FinalAuditCaseParams
): Promise<ApiResponse<void>> => {
  return request.post(
    `${CONFIG.baseURL}/qtong/mng/uploadRecordEntrust/final/audit`,
    params
  );
};

// 3. 案件管理相关接口

// 获取案件列表
export const getCaseList = (
  params: GetCaseListParams
): Promise<ApiResponse<GetCaseListResult>> => {
  return request.post(
    `${CONFIG.baseURL}/blackhole/mng/pressLoan/getOnePage`,
    params
  );
};

// 获取委案撤案列表
export const getEntrustRetractList = (
  params: GetEntrustRetractListParams
): Promise<ApiResponse<GetEntrustRetractListResult>> => {
  return request.post(
    `${CONFIG.baseURL}/blackhole/mng/pressLoan/getOnePage`,
    params
  );
};

// 委案初始化
export const countEntrustClient = (params: any): Promise<ApiResponse<any>> => {
  return request.post(
    `${CONFIG.baseURL}/blackhole/mng/entrust/countEntrustClient`,
    params
  );
};

// 获取案件基本信息
export const getUploadRecordEntrustInfo = (params: {
  id: string;
}): Promise<ApiResponse<any>> => {
  return request.post(
    `${CONFIG.baseURL}/qtong/mng/uploadRecordEntrust/info`,
    params
  );
};

// 预审接口
export const auditUploadRecordEntrust = (
  params: any
): Promise<ApiResponse<any>> => {
  return request.post(
    `${CONFIG.baseURL}/qtong/mng/uploadRecordEntrust/audit`,
    params
  );
};

// 终审接口
export const finalAuditUploadRecordEntrust = (
  params: any
): Promise<ApiResponse<any>> => {
  return request.post(
    `${CONFIG.baseURL}/qtong/mng/uploadRecordEntrust/final/audit`,
    params
  );
};

// 获取案件详情
export const getUploadCaseDetail = (params: any): Promise<ApiResponse<any>> => {
  return request.post(`${CONFIG.baseURL}/qtong/mng/entrust/uploadCase/get`, params);
};

// 获取批次信息
export const getUploadBatchByStateFilterEntrust = (
  params: any
): Promise<ApiResponse<any>> => {
  return request.post(
    `${CONFIG.baseURL}/blackhole/mng/uploadBatch/getByStateFilterEntrust`,
    params
  );
};

// 获取委案商列表
export const getEntrustByAId = (params: any): Promise<ApiResponse<any>> => {
  return request.post(`${CONFIG.baseURL}/authority/mng/entrust/getByAId`, params);
};

// 委案提交
export const entrustClient = (params: any): Promise<ApiResponse<any>> => {
  return request.post(
    `${CONFIG.baseURL}/blackhole/mng/entrust/entrustClient`,
    params
  );
};

// 获取委案摘要列表
export const getEntrustAUploadRecordList = (
  params: any
): Promise<ApiResponse<any>> => {
  return request.post(`${CONFIG.baseURL}/qtong/mng/entrustA/uploadRecord/list`, params);
};





// 4. 状态码常量
export const RESPONSE_STATUS: ResponseStatus = {
  SUCCESS: "200",
  BAD_REQUEST: "400",
  UNAUTHORIZED: "401",
  FORBIDDEN: "403",
  NOT_FOUND: "404",
  SERVER_ERROR: "500",
};

export const DISTRIBUTE_STATUS: DistributeStatusType = {
  INIT: {
    code: "INIT",
    message: "待分案",
  },
  RETRACT: {
    code: "RETRACT",
    message: "分案后撤案",
  },
};

// 5. 用户认证相关接口

// 用户登录
export const login = (params: LoginParams): Promise<LoginResult> => {
  return request.post(`${CONFIG.baseURL}/authority/mng/user/login`, params);
};

// 机构登录（SMS验证码）
export const loginSms = (params: LoginSmsParams): Promise<ApiResponse<LoginSmsResult>> => {
  return request.post(`${CONFIG.baseURL}/authority/anon/account/loginSms`, params);
};



// 用户登出
export const logout = (): Promise<ApiResponse<void>> => {
  return request.post(`${CONFIG.baseURL}/authority/mng/user/logout`);
};

// 刷新token
export const refreshToken = (): Promise<
  ApiResponse<{ token: string; expiresIn: number }>
> => {
  return request.post(`${CONFIG.baseURL}/authority/mng/user/refresh-token`);
};

// ==================== 分案相关接口 ====================

// 6. 分案初始化
export const countDistributeOne = (params: CountDistributeOneParams): Promise<ApiResponse<CountDistributeOneResult>> => {
  return request.post(`${CONFIG.baseURL}/blackhole/mng/pressCase/countDistributeOne`, params);
};

// 7. 初始化组织架构
export const getOrgList = (): Promise<ApiResponse<GetOrgListResult>> => {
  return request.post(`${CONFIG.baseURL}/authority/mng/org/list`);
};

// 8. 根据组织架构ID获取组织人员列表
export const queryUserByOrg = (params: QueryUserByOrgParams): Promise<ApiResponse<QueryUserByOrgResult>> => {
  return request.post(`${CONFIG.baseURL}/authority/mng/account/queryUserByOrg`, params);
};

// 9. 分案提交
export const distributeOne = (params: DistributeOneParams): Promise<ApiResponse<DistributeOneResult>> => {
  return request.post(`${CONFIG.baseURL}/blackhole/mng/pressCase/distributeOne`, params);
};

// ==================== 录案相关接口 ====================

// 10. 获取录案列表
export const getRecordCaseList = (params: GetRecordCaseListParams): Promise<ApiResponse<GetRecordCaseListResult>> => {
  return request.post(`${CONFIG.baseURL}/qtong/mng/entrustB/uploadRecord/list`, params);
};

// 11. 获取渠道列表
export const getChannelList = (params: GetChannelListParams): Promise<ApiResponse<GetChannelListResult>> => {
  return request.post(`${CONFIG.baseURL}/authority/mng/entrust/getChannelListByEntrustBId`, params);
};

// 12. 根据渠道获取产品类型列表
export const getProductList = (params: GetProductListParams): Promise<ApiResponse<GetProductListResult>> => {
  return request.post(`${CONFIG.baseURL}/authority/mng/entrust/getProductListByChannelAId`, params);
};

// 13. 录案提交接口
export interface SaveRecordCaseParams {
  bizChannel: string;
  productTypeName: string;
  delegateDate: string;
  recallDate: string;
  memo: string;
  id?: string;
  bizChannelName: string;
  tsToken: string;
}

export interface SaveRecordCaseResult {
  code: string;
  success: boolean;
  message: string;
}

export const saveRecordCase = (params: SaveRecordCaseParams): Promise<ApiResponse<SaveRecordCaseResult>> => {
  return request.post(`${CONFIG.baseURL}/qtong/mng/entrust/uploadRecord/save`, params);
};

// 14. 创建案件ID接口
export interface GetCaseIdParams {
  tsToken: string;
}

export interface GetCaseIdResult {
  code: string;
  success: boolean;
  message: string;
  result: string;
}

export const getCaseId = (params: GetCaseIdParams): Promise<ApiResponse<GetCaseIdResult>> => {
  return request.post(`${CONFIG.baseURL}/qtong/anon/id/get`, params);
};

// 撤委案相关接口类型定义
export interface ClientId {
  channelId: string;
  clientId: string;
}

export interface CountDelClientParams {
  clientIds: ClientId[];
  tsToken: string;
}

export interface CountDelClientResult {
  clientNum: number;
  errorClientNum: number;
  loanNum: number;
}

export interface DelClientParams {
  clientIds: ClientId[];
  tsToken: string;
}

export interface DelClientResult {
  message: string;
}

// 初始化撤委案接口
export const countDelClient = (params: CountDelClientParams): Promise<ApiResponse<CountDelClientResult>> => {
  return request.post(`${CONFIG.baseURL}/blackhole/mng/entrust/loan/countDelClient`, params);
};

// 撤委案提交接口
export const delClient = (params: DelClientParams): Promise<ApiResponse<DelClientResult>> => {
  return request.post(`${CONFIG.baseURL}/blackhole/mng/entrust/loan/delClient`, params);
};

// ==================== 一键委案相关接口 ====================

// 一键委案参数类型
export interface OneClickEntrustParams {
  // 案件基本信息
  batchName: string;
  entrustBName: string;
  // 委案商信息
  entrustBId: string;
  isInvalidRule: boolean;
  // 文件信息
  fileName: string;
  fileUrl: string;
  fileType: string;
  // 其他参数
  tsToken: string;
}

// 一键委案结果类型
export interface OneClickEntrustResult {
  success: boolean;
  message: string;
  caseId?: string;
  batchId?: string;
  steps: {
    preAudit: { success: boolean; message: string };
    finalAudit: { success: boolean; message: string };
    entrust: { success: boolean; message: string };
    scheduledTask: { success: boolean; message: string };
  };
}

// 一键委案接口
export const oneClickEntrust = (params: OneClickEntrustParams): Promise<ApiResponse<OneClickEntrustResult>> => {
  return request.post(`${CONFIG.baseURL}/qtong/mng/uploadRecordEntrust/oneClickEntrust`, params);
};

